Incorporation of stochastic chemistry on dust grains in the Meudon PDR code using moment equations I. Application to the formation of H2 and HD