As water and sunshine are both free, and the process isn't polluting, and burning Hydrogen doesn't cause global warming the economics are favourable.
Yes, but… Almost all hydrogen is currently produced from methane – a fossil fuel – which does cause. CO2 pollution. The only advantages of using that source of hydrogen is to reduce (to zero) the pollution at the point of use, and as a ‘bridge’ between electrified sections.
Currently, producing hydrogen by electrolysis is barely a 65% efficient process, let alone the inefficiency of the fuel cells (less than 70% efficiency?) Using electricity from source to drive the motors, or charge the battery (still needed in this scenario) makes much more sense – at the present time – until there are relatively huge surpluses of ‘green’ electricity available.
Even Scotland, which can generate more than it needs for its grid, overall, still does not produce all of its requirement from wind/solar generation. No sunshine at night and the wind does not blow evenly all the time.