In ERA5 data, some discrepancies can be found between the humidity (and temperature) at 2m and these variables on pressure levels due to two reasons.

The first reason is that the surface analysis (including at the 2m level) is done separately to the upper air 4D-Var analysis used for the pressure level output. For more information, please see the IFS documentation (CY41R2 for ERA5):

https://www.ecmwf.int/en/publications/ifs-documentation

and go to Part II Data assimilation.

The second reason is that the IFS uses hybrid model levels in the upper air (not pressure levels). Therefore, the data on pressure levels has been interpolated from the model levels and is extrapolated if the pressure level is "underground" at the point in question. Any subsequent horizontal interpolation (such as that used to produce the 0.25x0.25 regular latitude/longitude, pressure level data on the CDS disks) can then smear the boundary between upper air and underground points.

While we do not provide specific humidity at 2m, it can be calculated from the 2m temperature, dew point temperature and surface pressure. There are descriptions of the necessary calculation to obtain the specific humidity, at:

https://confluence.ecmwf.int/display/CKB/ERA5%3A+data+documentation#ERA5:datadocumentation-Computationofnear-surfacehumidityandsnowcover

and

How to calculate hus at 2m (huss):