Often you have chains of invention, where it's not obvious from the outset that a specific outcome can be achieved by making that specific discovery or invention.
Other issues are cases where the profit to society is clear, but difficult to capture by any specific group (the reason why infrastructure is mostly built by governments), and cases where discoveries happen by chance because somebody was given the chance to "screw around" (for example the person who found the xz vulnerability probably couldn't have justified why he put engineering time into investigating a 0.5 second delay).
That's a very bad take. Things have value even if they're not profitable. Do you think the pharaohs built the pyramids because they were profitable? Look how important they are to the world culture.
The most familiar thing to compare the pyramids to is probably European cathedrals. They were huge public works projects requiring a lot of labour, both skilled and unskilled, and done for religous, political, and probably economic reasons.
It's hard to fund things where the benefit is diffused among many people. Lighthouses are a classic example of this (you can enjoy their benefit even if you don't pay!). So is public art (why should I pay for public sculptures for everybody else?).
Taxes are a traditional approach to this. It's sloppy and imperfect (plenty of my money is spent on stuff I don't value), but I don't know a much better way.
Similarly, it's hard to price things where the negative externalities are diffused among many people. Pollution is a good example of this - the person generating pollution is often not the one suffering the effects of it, and the effects are generally widespread and diffuse (your car makes a thousand people a little bit worse off, not a single person horribly worse off). Taxes on these externalities are also an appropriate tool here.
You gotta think about profitability across a larger timescale. Some things aren’t profitable now, but will be later, and if we get started on them now we can make that later come sooner.
Other issues are cases where the profit to society is clear, but difficult to capture by any specific group (the reason why infrastructure is mostly built by governments), and cases where discoveries happen by chance because somebody was given the chance to "screw around" (for example the person who found the xz vulnerability probably couldn't have justified why he put engineering time into investigating a 0.5 second delay).