def rename(ds, **names):
    """
    Rename all variables etc that have an entry in names

    Parameters
    ----------
    ds : xarray Dataset
        A dataset to be renamed
    names : dict
        Dictionary of {old_name: new_name}
    """
    for k, v in names.items():
        if k in ds:
            if v in ds:
                # New name already exists
                if all(ds[k].values == ds[v].values):
                    ds = (
                        ds.assign_coords({v: ds[v].rename({v: k})})
                        .swap_dims({k: v})
                        .drop(k)
                    )
            else:
                ds = ds.rename({k: v})
    return ds