def with_metadata(self, metadata: Optional[dict]) -> Self:
        """
        Adds metadata to the TimeSeries.

        Parameters
        ----------
        metadata
            A dictionary with metadata to be added to the TimeSeries.

        Returns
        -------
        TimeSeries
            A new TimeSeries with the given metadata.

        Examples
        --------
        >>> from darts.utils.timeseries_generation import linear_timeseries
        >>> series = linear_timeseries(length=3)
        >>> # add metadata
        >>> metadata = {'name': 'my_series'}
        >>> series = series.with_metadata(metadata)
        >>> series.metadata
        {'name': 'my_series'}
        """

        return self.__class__(
            xr.DataArray(
                self._xa.values,
                dims=self._xa.dims,
                coords=self._xa.coords,
                attrs={
                    STATIC_COV_TAG: self.static_covariates,
                    HIERARCHY_TAG: self.hierarchy,
                    METADATA_TAG: metadata,
                },
            )
        )

        Adds metadata to the TimeSeries.

        Parameters
        ----------
        metadata
            A dictionary with metadata to be added to the TimeSeries.

        Returns
        -------
        TimeSeries
            A new TimeSeries with the given metadata.

        Examples
        --------
        >>> from darts.utils.timeseries_generation import linear_timeseries
        >>> series = linear_timeseries(length=3)
        >>> # add metadata
        >>> metadata = {'name': 'my_series'}
        >>> series = series.with_metadata(metadata)
        >>> series.metadata
        {'name': 'my_series'}
        
darts/timeseries.py