A search is presented for decays of the Higgs and boson to pairs of or (, with the subsequent decay of the mesons to . The analysis uses data from proton-proton collisions with an integrated luminosity of 37.5~\mathrm{fb}^{-1} at \sqrt{s}=13~\mathrm{TeV} collected with the CMS detector at the LHC in 2017. An observation of such a decay with this sample would indicate the presence of physics beyond the standard model. No significant excess of events is observed and confidence level upper limits are placed on the branching fractions. In the pair final state the limits are and for the Higgs and boson, respectively, while in the combined pair final states the limits are and , respectively. The and mesons from the Higgs and decay are assumed unpolarized. With longitudinally polarized and mesons in the Higgs and decay the upper limits reduce by about , while with transversely polarized mesons the decay limits increase by about .