You’re correct that mercury exhibits thermal expansion, but it’s not enough to affect a mercury barometer reading for most purposes. Sometimes the degree of precision of pressure measurements is such that temperature variations will make a difference. In this case a mathematical correction is made to the measured height of the mercury column to account for the change in volume. A table of corrections is shown below for temperatures above the calibration temperature of 0 degrees C. It shows the correction for 760 mm Hg at 20 degrees C to be 2.47 mm. This is a difference of only about 0.3%.
Mercury barometer readings are also affected by altitude, because the height to which air pressure will push a column of mercury depends on the local value gravity. This can also be mathematically corrected when required for a desired degree of precision.
You might be wondering why the expansion of mercury seems more significant in a thermometer than in a barometer. This is because the mercury in a thermometer is contained in a very thin tube. The small diameter of the tube causes a more noticeable change in height when the volume of the mercury changes.