def _temp_dataarray(ds: Dataset, y: Hashable, locals_: dict[str, Any]) -> DataArray:
    """Create a temporary datarray with extra coords."""
    from xarray.core.dataarray import DataArray

    coords = dict(ds[y].coords)
    dims = set(ds[y].dims)

    # Add extra coords to the DataArray from valid kwargs, if using all
    # kwargs there is a risk that we add unnecessary dataarrays as
    # coords straining RAM further for example:
    # ds.both and extend="both" would add ds.both to the coords:
    valid_coord_kwargs = {"x", "z", "markersize", "hue", "row", "col", "u", "v"}
    coord_kwargs = locals_.keys() & valid_coord_kwargs
    for k in coord_kwargs:
        key = locals_[k]
        darray = ds.get(key)
        if darray is not None:
            coords[key] = darray
            dims.update(darray.dims)

    # Trim dataset from unnecessary dims:
    ds_trimmed = ds.drop_dims(ds.sizes.keys() - dims)  # TODO: Use ds.dims in the future

    # The dataarray has to include all the dims. Broadcast to that shape
    # and add the additional coords:
    _y = ds[y].broadcast_like(ds_trimmed)

    return DataArray(_y, coords=coords)
Create a temporary datarray with extra coords.
xarray/plot/dataset_plot.py