Read MODIS HDF4 as xarray

import xarray as xr
from osgeo import gdal
xr.set_options(display_style='html')
%matplotlib inline

Find full subdataset names

def get_subdataset(file, varnames):
    g = gdal.Open(path)
    subdatasets = g.GetSubDatasets()
    l = []
    for varname in varnames:
        l.extend(  [s[0] for s in subdatasets if varname in s[0].split(':')[-1]  ])
    return l

Read subdatasets in xarray

def read_hdf4(file, varnames):
    ld = []
    fname_list = get_subdataset(file, varnames)
    for fname in fname_list:
        myDataset = xr.open_rasterio(fname)
        ld.append(myDataset.to_dataset(name=fname.split(':')[-1]))
    return xr.merge(ld)
varnames = ['cloud_top_temperature_1km', 'Cloud_Effective_Radius', 'Cloud_Optical_Thickness', 'Cloud_Multi_Layer_Flag']
path = 'MYD06_L2.A2019001.1850.061.2019002154022.hdf'
dset = read_hdf4(path, varnames)
/opt/conda/envs/pangeo/lib/python3.8/site-packages/rasterio/__init__.py:221: NotGeoreferencedWarning: Dataset has no geotransform set. The identity matrix may be returned.
  s = DatasetReader(path, driver=driver, sharing=sharing, **kwargs)
dset
<xarray.Dataset>
Dimensions:                                   (band: 1, x: 1354, y: 2030)
Coordinates:
  * band                                      (band) int64 1
  * y                                         (y) float64 0.5 1.5 ... 2.03e+03
  * x                                         (x) float64 0.5 1.5 ... 1.354e+03
Data variables:
    cloud_top_temperature_1km                 (band, y, x) int16 ...
    Cloud_Effective_Radius                    (band, y, x) int16 ...
    Cloud_Effective_Radius_PCL                (band, y, x) int16 ...
    Cloud_Effective_Radius_16                 (band, y, x) int16 ...
    Cloud_Effective_Radius_16_PCL             (band, y, x) int16 ...
    Cloud_Effective_Radius_37                 (band, y, x) int16 ...
    Cloud_Effective_Radius_37_PCL             (band, y, x) int16 ...
    Cloud_Effective_Radius_1621               (band, y, x) int16 ...
    Cloud_Effective_Radius_1621_PCL           (band, y, x) int16 ...
    Cloud_Effective_Radius_Uncertainty        (band, y, x) int16 ...
    Cloud_Effective_Radius_Uncertainty_16     (band, y, x) int16 ...
    Cloud_Effective_Radius_Uncertainty_37     (band, y, x) int16 ...
    Cloud_Effective_Radius_Uncertainty_1621   (band, y, x) int16 ...
    Cloud_Optical_Thickness                   (band, y, x) int16 ...
    Cloud_Optical_Thickness_PCL               (band, y, x) int16 ...
    Cloud_Optical_Thickness_16                (band, y, x) int16 ...
    Cloud_Optical_Thickness_16_PCL            (band, y, x) int16 ...
    Cloud_Optical_Thickness_37                (band, y, x) int16 ...
    Cloud_Optical_Thickness_37_PCL            (band, y, x) int16 ...
    Cloud_Optical_Thickness_1621              (band, y, x) int16 ...
    Cloud_Optical_Thickness_1621_PCL          (band, y, x) int16 ...
    Cloud_Optical_Thickness_Uncertainty       (band, y, x) int16 ...
    Cloud_Optical_Thickness_Uncertainty_16    (band, y, x) int16 ...
    Cloud_Optical_Thickness_Uncertainty_37    (band, y, x) int16 ...
    Cloud_Optical_Thickness_Uncertainty_1621  (band, y, x) int16 ...
    Cloud_Multi_Layer_Flag                    (band, y, x) uint8 ...