Repository: mne-tools/mne-python

Issue:
mne.channels.combine_channels(epochs) drops metadata
I think `mne.channels.combine_channels(epochs)` drops metadata (i.e. there is no `epochs.metadata` attribute in the resulting `Epochs` objects). I wanted to make sure that this is not intended – if it is a bug I'm happy to submit a PR.


Discussion:
Reprex:

```python
import mne

root = mne.datasets.sample.data_path() / 'MEG' / 'sample'
raw = mne.io.read_raw_fif(root / 'sample_audvis_filt-0-40_raw.fif', preload=False)
events = mne.read_events(root / 'sample_audvis_filt-0-40_raw-eve.fif')
raw.crop(tmax=90)
raw.pick(['eeg', 'eog']).load_data()
raw.rename_channels({name: name.replace(' 0', '').lower() for name in raw.ch_names})

event_dict = {
    'auditory/left': 1,
    'auditory/right': 2,
    'visual/left': 3,
    'visual/right': 4,
    'face': 5,
    'buttonpress': 32
}

metadata, events, event_id = mne.epochs.make_metadata(
    events=events,
    event_id=event_dict,
    tmin=-0.3,
    tmax=0.7,
    sfreq=raw.info['sfreq']
)

epochs = mne.Epochs(
    raw,
    events,
    event_id=event_dict,
    tmin=-0.3,
    tmax=0.7,
    metadata=metadata,
    preload=True
)

print(epochs.metadata)  # metadata is still there
print(
    mne.channels.combine_channels(
        epochs,
        {"ROI1": mne.pick_channels(epochs.ch_names, include=["eeg01", "eeg02", "eeg03"])}
    ).metadata  # metadata is gone
)
```

Resolving patch:
diff --git a/mne/channels/channels.py b/mne/channels/channels.py
--- a/mne/channels/channels.py
+++ b/mne/channels/channels.py
@@ -1710,6 +1710,8 @@ def combine_channels(inst, groups, method='mean', keep_stim=False,
     elif isinstance(inst, BaseEpochs):
         combined_inst = EpochsArray(new_data, info, events=inst.events,
                                     tmin=inst.times[0])
+        if inst.metadata is not None:
+            combined_inst.metadata = inst.metadata.copy()
     elif isinstance(inst, Evoked):
         combined_inst = EvokedArray(new_data, info, tmin=inst.times[0])
 


Test patch:
diff --git a/mne/channels/tests/test_channels.py b/mne/channels/tests/test_channels.py
--- a/mne/channels/tests/test_channels.py
+++ b/mne/channels/tests/test_channels.py
@@ -24,7 +24,7 @@
 from mne import (pick_types, pick_channels, EpochsArray, EvokedArray,
                  make_ad_hoc_cov, create_info, read_events, Epochs)
 from mne.datasets import testing
-from mne.utils import requires_version
+from mne.utils import requires_pandas, requires_version
 
 io_dir = op.join(op.dirname(__file__), '..', '..', 'io')
 base_dir = op.join(io_dir, 'tests', 'data')
@@ -521,3 +521,20 @@ def test_combine_channels():
         combine_channels(raw, warn2)
         combine_channels(raw_ch_bad, warn3, drop_bad=True)
     assert len(record) == 3
+
+
+@requires_pandas
+def test_combine_channels_metadata():
+    """Test if metadata is correctly retained in combined object."""
+    import pandas as pd
+
+    raw = read_raw_fif(raw_fname, preload=True)
+    epochs = Epochs(raw, read_events(eve_fname), preload=True)
+
+    metadata = pd.DataFrame({"A": np.arange(len(epochs)),
+                             "B": np.ones(len(epochs))})
+    epochs.metadata = metadata
+
+    good = dict(foo=[0, 1, 3, 4], bar=[5, 2])  # good grad and mag
+    combined_epochs = combine_channels(epochs, good)
+    pd.testing.assert_frame_equal(epochs.metadata, combined_epochs.metadata)