def validate_dataarray_coords(
    shape: tuple[int, ...],
    coords: Coordinates | Mapping[Hashable, Variable],
    dim: tuple[Hashable, ...],
):
    """Validate coordinates ``coords`` to include in a DataArray defined by
    ``shape`` and dimensions ``dim``.

    If a coordinate is associated with an index, the validation is performed by
    the index. By default the coordinate dimensions must match (a subset of) the
    array dimensions (in any order) to conform to the DataArray model. The index
    may override this behavior with other validation rules, though.

    Non-index coordinates must all conform to the DataArray model. Scalar
    coordinates are always valid.
    """
    sizes = dict(zip(dim, shape, strict=True))
    dim_set = set(dim)

    indexes: Mapping[Hashable, Index]
    if isinstance(coords, Coordinates):
        indexes = coords.xindexes
    else:
        indexes = {}

    for k, v in coords.items():
        if k in indexes:
            invalid = not indexes[k].should_add_coord_to_array(k, v, dim_set)
        else:
            invalid = any(d not in dim for d in v.dims)

        if invalid:
            raise CoordinateValidationError(
                f"coordinate {k} has dimensions {v.dims}, but these "
                "are not a subset of the DataArray "
                f"dimensions {dim}"
            )

        for d, s in v.sizes.items():
            if d in sizes and s != sizes[d]:
                raise CoordinateValidationError(
                    f"conflicting sizes for dimension {d!r}: "
                    f"length {sizes[d]} on the data but length {s} on "
                    f"coordinate {k!r}"
                )
Validate coordinates ``coords`` to include in a DataArray defined by
    ``shape`` and dimensions ``dim``.

    If a coordinate is associated with an index, the validation is performed by
    the index. By default the coordinate dimensions must match (a subset of) the
    array dimensions (in any order) to conform to the DataArray model. The index
    may override this behavior with other validation rules, though.

    Non-index coordinates must all conform to the DataArray model. Scalar
    coordinates are always valid.
    
xarray/core/coordinates.py