We present new empirical constraints on the evolution of ρH2, the cosmological mass density of molecular hydrogen, back to z≈2.5. We employ a statistical approach measuring the average observed 850μm flux density of near-infrared selected galaxies as a function of redshift. The redshift range considered corresponds to a span where the 850μm band probes the Rayleigh-Jeans tail of thermal dust emission in the rest-frame, and can therefore be used as an estimate of the mass of the interstellar medium (ISM). Our sample comprises of ≈150,000 galaxies in the UKIDSS-UDS field with near-infrared magnitudes KAB≤25 mag and photometric redshifts with corresponding probability distribution functions derived from deep 12-band photometry. With a sample approximately 2 orders of magnitude larger than in previous works we significantly reduce statistical uncertainties on ρH2 to z≈2.5. Our measurements are in broad agreement with recent direct estimates from blank field molecular gas surveys, finding that the epoch of molecular gas coincides with the peak epoch of star formation with ρH2≈2×107M⊙Mpc−3 at z≈2. We demonstrate that ρH2 can be broadly modelled by inverting the star-formation rate density with a fixed or weakly evolving star-formation efficiency. This 'constant efficiency' model shows a similar evolution to our statistically derived ρH2, indicating that the dominant factor driving the peak star formation history at z≈2 is a larger supply of molecular gas in galaxies rather than a significant evolution of the star-formation rate efficiency within individual galaxies.