def z_coord(cube):
    """
    Return the canonical vertical coordinate.

    Examples
    --------
    >>> import iris
    >>> url = ('http://omgsrv1.meas.ncsu.edu:8080/thredds/dodsC/'
    ...        'fmrc/us_east/US_East_Forecast_Model_Run_Collection_best.ncd')
    >>> cube = iris.load_cube(url, 'sea_water_potential_temperature')
    >>> zvar = z_coord(cube)
    >>> zvar.name()
    'ocean_s_coordinate_g2'
    >>> cube.coord_dims(zvar)
    (1,)

    """
    # http://cfconventions.org/cf-conventions/v1.6.0/cf-conventions.html#dimensionless-v-coord
    dimensionless = [
        "atmosphere_hybrid_height_coordinate",
        "atmosphere_hybrid_sigma_pressure_coordinate",
        "atmosphere_sigma_coordinate",
        "atmosphere_sleve_coordinate",
        "ocean_s_coordinate",
        "ocean_s_coordinate_g1",
        "ocean_s_coordinate_g2",
        "ocean_sigma_coordinate",
        "ocean_sigma_z_coordinate",
    ]

    # Setting `dim_coords=True` to avoid triggering the download
    # of the derived `z`. That usually throws a Memory error as it
    # varies with `t`, `x`, `y`, and `z`.
    zvars = cube.coords(axis="Z", dim_coords=True)
    if not zvars:
        zvars = cube.coords(axis="altitude", dim_coords=True)

    # Could not find a dimension coordinate.
    if not zvars:
        zvars = [
            coord
            for coord in cube.coords(axis="Z")
            if coord.name() in dimensionless
        ]

    if not zvars:
        raise ValueError(f"Could not find the vertical coordinate!")

    if len(zvars) > 1:
        raise ValueError(f"Found more than one vertical coordinate: {zvars}")
    else:
        zvar = zvars[0]

    return zvar