Why not just pass a law that no one can generate electricity except from green sources? It sounds so easy when I put it like that.
Um - those laws have been passed in many countries. Usually with a reasonable approach such as "you can continue operating the coal plants that were already built, but no more can be built".
What's actually happening around the world though is those plants are becoming too expensive to run, so they're shutting down even if they are allowed to continue to operate. Renewable power is just cheaper.
About two thirds of global electricity production is zero emission now and it'll be around 95% in a 25 years or so.
Source (note: this is a "renewables" article, not a "zero emission" article. Some non-renewable energy produces zero emissions and there's not expected to be much movement on that in the foreseeable future): https://renewablesnow.com/news/renewables-produce-85-of-global-power-nearly-50-of-energy-in-2050-582235/