def _construct_dataarray(self, name: Hashable) -> DataArray:
        """Construct a DataArray by indexing this dataset"""
        from xarray.core.dataarray import DataArray

        try:
            variable = self._variables[name]
        except KeyError:
            _, name, variable = _get_virtual_variable(self._variables, name, self.sizes)

        needed_dims = set(variable.dims)

        coords: dict[Hashable, Variable] = {}
        # preserve ordering
        for k in self._variables:
            if k in self._indexes:
                add_coord = self._indexes[k].should_add_coord_to_array(
                    k, self._variables[k], needed_dims
                )
            else:
                var_dims = set(self._variables[k].dims)
                add_coord = k in self._coord_names and var_dims <= needed_dims

            if add_coord:
                coords[k] = self._variables[k]

        indexes = filter_indexes_from_coords(self._indexes, set(coords))

        return DataArray(variable, coords, name=name, indexes=indexes, fastpath=True)
Construct a DataArray by indexing this dataset
xarray/core/dataset.py