The origin of rovibrational H_2 emission in the central galaxies of cooling flow clusters is poorly understood. Here we address this issue using data from our near-infrared spectroscopic survey of 32 of the most line luminous such systems, presented in the companion paper by Edge et al. (2002). We consider excitation by X-rays from the surrounding intracluster medium, UV radiation from young stars, and shocks. The v=1-0 K-band lines with upper levels within 10^4 K of the ground state appear to be mostly thermalised (implying gas densities above ~10^5 cm^-3), with excitation temperatures typically exceeding 2000K, as found earlier by Jaffe, Bremer and van der Werf (2001). Together with the lack of strong v=2-0 lines in the H-band, this rules out UV fluorescence. Using the CLOUDY photoionisation code, we deduce that the H_2 lines can originate in a population of dense clouds, exposed to the same hot (T~50000K) stellar continuum as the lower density gas which produces the bulk of the forbidden optical line emission in the H-alpha luminous systems. This dense gas may be in the form of self-gravitating clouds deposited directly by the cooling flow, or may instead be produced in the high-pressure zones behind strong shocks. Furthermore, the shocked gas is likely to be gravitationally unstable, so collisions between the larger clouds may lead to the formation of globular clusters.