You're asking two questions here but they are related, so that may be OK.
The mass ratio of the Sun is probably not 3:1 though, as the Sun has been turning hydrogen into Helium for nearly 5 billion years. The interior of the Sun has a considerably higher helium ratio as a result, but the outer edges of the sun - which is what we see and read from spectroscopy, is close to the 3:1 mass ratio.
If this is accurate the Sun overall is closer to about 2.6:1.
Scientists can measure the ratio by observing spectral lines. Stars make this easy because of how hot they are, but that information is limited to their surface. We can't see the core of the sun. Still more than half of the elements have been observed in the solar surface.
See on solar spectroscopy
Or a related question from Physics Stack
The 25% helium by mass is confirmed both by spectroscopy and by quantum physics models based on the modeled expansion as the early Universe goes from hot to cold. Helium is more stable than hydrogen, but, it requires an intermediate step in it's formation because deuterium has to form first. Also, though I think this plays a smaller role, free neutrons decay and become protons which become Hydrogen.
More massive elements need hotter temperatures to form but the early universe went from hot (Quark soup), to less hot (formation of hadrons), to static - 92% Hydrogen, 8% helium by element or 75-25 by mass with tiny trace amounts of Lithium. It stayed that way until the first stars and the formation of heavier elements.
That's a very simplified version. You can visit the stack exchange answer below for more details or this link here.
Both spectroscopy and quantum physics agree on the ratio, which is always a plus when the same answer is reached two different ways. There is a lingering lithium ratio problem, but even so, the 75%-25% ratio is pretty good.