Spectra from galaxies include both absorption and emission lines. I do understand how both types of spectral lines are produced but I am not quite sure where each type is coming from when we observe a galaxy. Few month ago I was told that emission lines are coming from the gas of the galaxy and absorption lines are coming from the stars of the galaxy. Is that correct? If it is, why the opposite doesn't happen? Why can't we have absorption lines coming from the gas of the galaxy? I am mostly (but not exclusively) interested in the Hydrogen lines. I have been looking at some spiral galaxy integral filed spectroscopy datacubes. I always see a very strong Ha emission line, never an Ha absorption line. Why?
A galaxy spectrum is a quite complex and complicated topic, and many entire careers are fully devoted to understanding them, so this can only be a simplified answer. It is still quite lengthy, though, so if you're impatient, I've summarised it at the bottom.
A blend of starlight of different spectral types makes up the continuum.
The light is emitted by stars in the galaxy. Stellar spectra are approximately a black body spectrum, but this mostly holds true for the most hot and luminous O and B type stars; for cooler stars, the image already gets quite a bit more muddled, as they will show more and more absorption happening in the stellar photosphere, as well as other effects that ruin the nice, smooth black body spectrum. These differences in spectra are actually the basis of the classification of stars in the spectral types O, B, A, F, G, K, M and L.
The spectrum of the galaxy, however, is a mix of all the stars within it. This will often be dominated by the hotter and heavier stars, but the other spectral types also blend in. There will generally be some stellar absorption features, but not many, and the combined light of the stars will often show a nice, relatively smooth profile. The shape of this composite spectrum depends heavily on the composition of stellar types - a young, irregular star-burst galaxy and an old, dying elliptical will show widely different shapes of the continuum.
Emission lines originate in the hot ISM and in stellar atmospheres
The galaxy will often have regions of star formation, in which the strong radiation ionize the surrounding interstellar medium, creating a so called HII Region. The ions and free electrons will recombine, and the excited states will decay, resulting in a cascade of electrons dropping between energy levels in the different atoms, resulting in a series of emission lines that is added to the spectrum. The energy radiated in all these lines doesn't come from the clouds themselves, but from the most energy-rich photons, so the gas will leave a drop in intensity on the blue side of these wavelengths in the spectrum (multiple drops of different strength, to be precise).
The absolute strengths of the emission lines depend on various parameters, of which star formation rate (SFR) is the most important - galaxies with ceased star formation show very few emission lines. The relative strengths of these lines depend on the chemical composition, temperature and other physical properties of the hot gas, so studying these line intensity ratios can often teach us a lot about the galaxies we observe. Besides, sometimes we can also see flourescent lines, when photons from the hot gas excite atoms in colder, non-ionized clouds which then re-emit them in slightly different wavelengths. The wavelength offsets here can also tell us about the internal kinematics of the galaxy.
Below is a figure from this article that shows an average spectrum from a large number of star forming galaxies. Note the drop in flux on the blue side of the Lyman-$\alpha$ line. Taking the average will enhance the features that they have in common while suppressing features that are peculiar to individual galaxies. The caption below is mine from my Bachelor's project paper.
Absorption happens both in the cold, neutral ISM, the hot, ionized ISM and in stellar atmospheres
The very hot, ionized regions describes about are most likely to be found in the central regions of a galaxy. Further out, things are a bit cooler and calmer, and clouds of neutral hydrogen containing variable amounts of heavier elements are more numerous. When the combined light of the stars and the hot HII regions pass through these, some of it is absorbed by the colder atoms and re-emitted in random directions, with a probability of being re-emitted in the exact same direction it came in being practically zero. This means that these wavelengths will be deprived of photons, leaving the characteristic dark absorption lines in the spectrum - sometimes, but not always, at the same wavelengths where the stellar atmospheres have already done some of the absorption, so this needs to be taken into account when analysing a galactic spectrum.
Below is an example plot of absorption lines from multiple neutral gas clouds in a galaxy that I analysed for my Master's thesis. The light here comes from a quasar behind the galaxy, not the galaxy itself, but that has no impact on the absorbing gas. The green markers show the centres of the individual lines, corresponding to a peculiar velocity for each gas cloud.
Due to internal dynamics, turbulence and rotation etc., these clouds will generally have different velocities than the stars and HII regions, giving them a slight red- or blueshift w.r.t. these. Sometimes, we can distinguish slightly offset absorption line sets from several neutral gas clouds within the galaxy, which can reveal yet more information about its internal workings.
Absorption like this happens in the dense, cold, neutral ISM as well as the hotter, less dense but more voluminous, ionized ISM. Different ionization degrees of the same atom will leave different sets of lines, so the same atom can be used to trace both hot and cold ISM. The difference between the line strengths can be used to assess the ionization degree of the parts of the galaxy we're looking at.
Emission and absorption of different hydrogen lines is not equally likely, and happens in different places
When it comes to Hydrogen lines particularly, the answer is not simple because the lines are very different. You will probably see absorption in Lyman-$\alpha$, because this is a very strong line, that is, if a photon of the right wavelength meets an atom at the right energy state, an absorption is very likely to happen - and these atoms are the most abundant in the Universe. But Lyman-$\alpha$ is complicated, and often you are likely to see both emission and absorption together creating a complicated line profile. Other lines, like the Balmer lines, are not very strongly absorbing, but emit very strongly, so you will mostly see emission from these lines.
To see why this is, remember how emission and absorption happens. Lyman-$\alpha$ is the transition between the ground state and the first excited state of the hydrogen atom. In cold gas, hydrogen spends by far the most of its time in the ground state. That means that there are lots of atoms around ready to absorb in Ly$\alpha$. But once the absorption has happened, the electron doesn't sit around in the first excited state for long, but spontaneously re-emits the photons and decays back to the ground state. In the hot ISM, therefore, we can have Lyman-$\alpha$ emission from recombined atoms, and in the cold ISM, there is strong absorption happening.
Here is Wikipedia's illustration of the Lyman and Balmer series, for reference:
The first Balmer line, H$\alpha$, on the other hand, is the transition between the first and second excited state of Hydrogen. This doesn't happen much in absorption, because atoms are unlikely to sit around in the first excited state for long. On the other hand, when an atom is ionized and recombines, or just strongly excited by an energetic photon, its path back to the ground state is likely to take it through one of the Balmer series transitions and then the Lyman-$\alpha$ transition.
Therefore, in the hot ISM, we are very likely to have Balmer emission, while it is very unlikely to have Balmer absorption in the cold ISM. When we do see Balmer absorption, it is most often in the stellar photospheres, because the high temperatures there means that there are many more atoms in higher energy states, so a H$\alpha$-wavelength photon is much more likely to encounter an atom there that is sitting around in the first excited state.
Here is a composite image of the galaxy known as ESO 338 IG04, observed in 3 different filters, that very nicely illustrates the origin of different kinds of radiation. It is taken from my supervisor's web page.
stars themselves emit a continuous spectrum because they are extremely hot and dense.
it is only gas between those stars and the observer that lead to emission or absorption "lines". in both cases the gases are absorbing and re-emitting the continuous spectrum of light coming from the star behind.
in cold/sparse gases the re-emitted radiation are too weak, or simply do not occur often enough that they come in our direction, leading to gaps in the supposed continous spectrum expected from the star behind--absorption spectra.
for the opposite case, in warmer/denser gases, the re-emissions are brighter than the attenuated spectrum coming from the star behind, leading to emission lines.