It's much different from open source software. Science and open source are in some ways similar: people do both because they love it, believe in it, and want to move us forward. But there are also major differences.
Scientists who pay mind to the competitive aspect of their field very rarely (but not never) have an ulterior commercial motive. They are just trying to survive. A scientist's whole career depends on their publication record. Reputable journals won't publish work that has already been published. Otherwise there would lots of duplicate work and less motivation to do new, harder, groundbreaking science. So, if someone "beats you to the punch," you can't publish. If you can't publish, you can't successfully compete for research funding. If you can't successfully compete for research funding, your career as an active scientist is over.
Another distinction between open source and science is that you can meaningfully contribute to open source software in your free time with a laptop and an internet connection. This is not possible in modern science. To do work that pushes the boundaries, you need expensive things like lasers, supercomputers, graduate students, and space telescopes. Nobody is going to give a weekend warrior with some spare time access to this stuff because it is too precious. If you want access, you need to compete for it. To successfully compete for it, you need a credible publication record. And here we are again where we began.
I will wrap up with the matter of publicly funded stuff being publicly available. I agree that this should be the case. However, it is important to appreciate that science goes nowhere without good questions. It is really hard to come up with good questions, which are generally formatted as a research proposal. I think it is reasonable to give the scientist who came up with the research proposal that motivated data collection of JWST some period of limited exclusivity on the data (6 months? 1 year?) so that they are able to develop publications and get credit for their work. The truth is that much of the scientific work has already been done by the time the telescope is collecting data. What movies like "Don't Look Up" or "Contact" don't show you is that before the astronomers go to the telescope to sit in the dark before screens full of data appear, they (or their PI) spent months carefully developing a plan/proposal for where they were going to point the telescope and what they were going to measure. Without that plan, the telescope is not very useful.