Context. Cosmic gas makes up about 90% of the baryonic matter in the Universe and H2 molecule is the most tightly linked to star formation. Aims. In this work we study cold neutral gas, its H2 component at different epochs, and corresponding depletion times. Methods. We perform state-of-the-art hydrodynamic simulations that include time-dependent atomic and molecular non-equilibrium chemistry coupled to star formation, feedback effects, different UV backgrounds presented in the recent literature and a number of additional processes occurring during structure formation (COLDSIM). We predict gas evolution and contrast the mass density parameters and gas depletion timescales. We also investigate their relation to cosmic expansion in light of the latest infrared and (sub)millimetre observations in the redshift range 2 ≲ z ≲ 7. Results. By performing updated non-equilibrium chemistry calculations we are able to broadly reproduce the latest HI and H2 observations. We find neutral-gas mass density parameters Ωneutral ≃ 10−3 and increasing from lower to higher redshift, in agreement with available HI data. Because of the typically low metallicities during the epoch of reionisation, time-dependent H2 formation is mainly led by the H− channel in self-shielded gas, while H2 grain catalysis becomes important in locally enriched sites at any redshift. Ultraviolet (UV) radiation provides free electrons and facilitates H2 build-up while heating cold metal-poor environments. Resulting H2 fractions can be as high as ∼50% of the cold gas mass at z ∼ 4–8, in line with the latest measurements from high-redshift galaxies. The H2 mass density parameter increases with time until a plateau of ΩH2 ≃ 10−4 is reached. Quantitatively, we find agreement between the derived ΩH2 values and the observations up to z ∼ 7 and both HI and H2 trends are better reproduced by our non-equilibrium H2-based star formation modelling. The predicted gas depletion timescales decrease at lower z in the whole time interval considered, with H2 depletion times remaining below the Hubble time and comparable to the dynamical time at all z. This implies that non-equilibrium molecular cooling is efficient at driving cold-gas collapse in a broad variety of environments and has done so since very early cosmic epochs. While the evolution of chemical species is clearly affected by the details of the UV background and gas self shielding, the assumptions on the adopted initial mass function, different parameterizations of H2 dust grain catalysis, photoelectric heating, and cosmic-ray heating can affect the results in a non-trivial way. In the Appendix, we show detailed analyses of individual processes, as well as simple numerical parameterizations and fits to account for them. Conclusions. Our findings suggest that, in addition to HI, non-equilibrium H2 observations are pivotal probes for assessing cold-gas cosmic abundances and the role of UV background radiation at different epochs.