def _overwrite_indexes(
        self,
        indexes: Mapping[Any, Index],
        variables: Mapping[Any, Variable] | None = None,
        drop_coords: list[Hashable] | None = None,
        rename_dims: Mapping[Any, Any] | None = None,
    ) -> Self:
        """Maybe replace indexes and their corresponding coordinates."""
        if not indexes:
            return self

        if variables is None:
            variables = {}
        if drop_coords is None:
            drop_coords = []

        new_variable = self.variable.copy()
        new_coords = self._coords.copy()
        new_indexes = dict(self._indexes)

        for name in indexes:
            new_coords[name] = variables[name]
            new_indexes[name] = indexes[name]

        for name in drop_coords:
            new_coords.pop(name)
            new_indexes.pop(name)

        if rename_dims:
            new_variable.dims = tuple(rename_dims.get(d, d) for d in new_variable.dims)

        return self._replace(
            variable=new_variable, coords=new_coords, indexes=new_indexes
        )
Maybe replace indexes and their corresponding coordinates.
xarray/core/dataarray.py